﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Emgu.CV.UI;
using Emgu.CV;
using Emgu.CV.Structure;
using iMapa.UI.ModelView;
using System.Windows.Forms.Integration;
using iMapa.Backend;
using iMapa.UI.ModelView.Common;

namespace iMapa.UI.View.Common
{
    /// <summary>
    /// Interaction logic for CameraCaptureView.xaml
    /// </summary>
    public partial class CameraCaptureView : UserControl
    {
        private ImageBox iw;
        private CameraCaptureViewModel vm;
        private bool disposed;



        delegate void SetImageCallback(Image<Bgr,Byte> i);

        public CameraCaptureView()
        {
            InitializeComponent();
            vm = (CameraCaptureViewModel)this.DataContext;
            
            
            vm.PropertyChanged += onPropChanged;
            
        }

        private void createImageBox()
        {
            WindowsFormsHost wfh = (WindowsFormsHost)this.FindName("host");
            iw = iw==null?new ImageBox():iw;
            iw.Height = (int)wfh.Height;
            iw.Width = (int)wfh.Width;
            iw.FunctionalMode = ImageBox.FunctionalModeOption.Minimum;
            double ratioW = (double)iw.Width / (double)vm.CapturedImage.Width;
            double ratioH = (double)iw.Height / (double)vm.CapturedImage.Height;
            double ratio = ratioH < ratioW ? ratioH : ratioW;
            iw.SetZoomScale(ratio, new System.Drawing.Point());
            wfh.Child = iw;
        }

        public void onPropChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("CapturedImage"))
            {
                if (iw != null && iw.Image != null && iw.Height != (int)(vm.CapturedImage.Height * iw.ZoomScale))
                {
                    double ratioW = (double)iw.Width / (double)vm.CapturedImage.Width;
                    double ratioH = (double)iw.Height / (double)vm.CapturedImage.Height;
                    double ratio = ratioH < ratioW ? ratioH : ratioW;
                    try
                    {
                        iw.SetZoomScale(ratio, new System.Drawing.Point());
                    }catch(Exception ex){
                    }
                }
                SetImage(vm.CapturedImage);
            }
        }

        public void SetImage(Image<Bgr,Byte> i)
        {
            if (iw != null)
            {
                if (iw.InvokeRequired)
                {
                    SetImageCallback d = new SetImageCallback(SetImage);
                    Dispatcher.Invoke(d, new object[] { i });
                }
                else
                {
                    if (!disposed)
                    {
                        try
                        {
                            IImage oldImage = iw.Image;
                            iw.Image = vm.CapturedImage.Clone();
                            oldImage.Dispose();
                        }
                        catch (Exception e) { }
                    }

                }

            }
        }

        private void onUnload(object sender, RoutedEventArgs e)
        {
            disposed = true;
            vm.PauseCapture();
            CameraInput.RestartCapture();
         }

        private void onLoad(object sender, RoutedEventArgs e)
        {
            CameraInput.getInstance();
            vm.ResumeCapture();
            createImageBox();
            disposed = false;
        }
        
        public double CaptureHeight
        {
            get
            {
                WindowsFormsHost wfh = (WindowsFormsHost)this.FindName("host");
                return wfh.Height; 
              }
            set
            {
                WindowsFormsHost wfh = (WindowsFormsHost)this.FindName("host");
                wfh.Height=value;
            }
        }
        public double CaptureWidth
        {
            get
            {
                WindowsFormsHost wfh = (WindowsFormsHost)this.FindName("host");
                return wfh.Width;
            }
            set
            {
                WindowsFormsHost wfh = (WindowsFormsHost)this.FindName("host");
                wfh.Width = value;
            }
        }

    }
}
